package com.yan.test.work.testpaging3

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.recyclerview.widget.RecyclerView
import com.yan.test.work.testpaging3.databinding.LayoutLostLoadingFooterBinding

/**
 * Create By : 'YWY'
 * On : 2024/4/5
 */
class LoadingStatusAdapter: LoadStateAdapter<LoadingStatusAdapter.LoadingHolder>() {


    inner class LoadingHolder(val binding: LayoutLostLoadingFooterBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onBindViewHolder(holder: LoadingHolder, loadState: LoadState) {
        val loading = holder.binding.loading
        val loadingText = holder.binding.text

        when(loadState){
            is LoadState.Loading->{
                loadingText.setText("努力加载中....")
                loading.show()
                return
            }
            is LoadState.Error->{
                loadingText.setText("已经没有更多了")
            }
            else->{
            }
        }

        loading.hide()
        loading.postOnAnimation { loading.visibility = View.GONE }
    }

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): LoadingHolder {
        return LoadingHolder(LayoutLostLoadingFooterBinding.inflate(LayoutInflater.from(parent.context) , parent , false))

    }
}